//
//  MyPropertyController.m
//  ChickenA
//
//  Created by 韦显光 on 2018/7/5.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "MyPropertyController.h"
#import "MylistCell.h"
#import "SPPageMenu.h"
#import "MydetailedController.h"


@interface MyPropertyController ()<SPPageMenuDelegate, UIScrollViewDelegate>
{
    NSArray*dataary;
    int page;
    NSMutableArray*mary;

}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrW;
@property (weak, nonatomic) IBOutlet UIScrollView *scrview;
@property (weak, nonatomic) IBOutlet UIView *muneBack;
@property (nonatomic, weak) SPPageMenu *pageMenu;
@property (strong, nonatomic) IBOutletCollection(UITableView) NSArray *tbvs;
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *Hidens;


@end

@implementation MyPropertyController

-(void)viewDidAppear:(BOOL)animated{
    _pageMenu.selectedItemIndex=[_type integerValue];
}

-(void)loaddat
{
    
    [FuncPublic getData:self parameters:@{@"type":[NSString stringWithFormat:@"%ld",_pageMenu.selectedItemIndex]} RequestUrl:my_delivers success:^(NSDictionary *acountInfo) {
        NSArray*tempary=acountInfo[@"data"];
        UITableView*tabview=_tbvs[_pageMenu.selectedItemIndex];
        [tabview.mj_header endRefreshing];
        NSMutableArray*Tary=mary[_pageMenu.selectedItemIndex];
         UIView*hidenview=_Hidens[_pageMenu.selectedItemIndex];
        [Tary addObjectsFromArray:tempary];
        hidenview.hidden=Tary.count>0?YES:NO;
        [tabview reloadData];
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
     _scrW.constant=DEVW*5;
 
    NSArray*tempary=@[@"全部",@"任务进行",@"任务完成"];
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, DEVW, 50) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    pageMenu.itemTitleFont=[UIFont systemFontOfSize:14];
    [pageMenu setItems:tempary selectedItemIndex:0];
    pageMenu.permutationWay=SPPageMenuPermutationWayNotScrollEqualWidths;
    // 设置代理
    pageMenu.delegate = self;
    [_muneBack addSubview:pageMenu];
    _pageMenu = pageMenu;
    mary =[NSMutableArray array];
    [tempary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UITableView*tabview=_tbvs[idx];
        tabview.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            NSMutableArray*tempMary= mary[idx];
            [tempMary removeAllObjects];
            page=1;
            [self loaddat];
        }];
    
        
        tabview.separatorColor=[UIColor clearColor];
        [mary addObject:[NSMutableArray array]];
    }];
    
    // Do any additional setup after loading the view.
}
#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView==_scrview) {
        [self.pageMenu moveTrackerFollowScrollView:scrollView];
    }
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    if (labs(toIndex - fromIndex) >= 2) {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:NO];
    } else {
        [_scrview setContentOffset:CGPointMake(DEVW * toIndex, 0) animated:YES];
    }
    page=1;
    NSMutableArray*Tary=mary[_pageMenu.selectedItemIndex];
    [Tary removeAllObjects];
    [self loaddat];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
     NSMutableArray*Tary=mary[_pageMenu.selectedItemIndex];
     NSDictionary*dic=Tary[indexPath.section];
     MylistCell*cell=[MylistCell CellWithTableView:tableView];
    [cell Cellinit:dic];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.completion = ^(UIButton *changgeBtn) {
        [tableView.mj_header beginRefreshing];
    };
    return cell;

}
#pragma mark 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
      NSMutableArray*Tary=mary[_pageMenu.selectedItemIndex];
    return Tary.count;
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  1;
}


#pragma mark 设置分组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}


#pragma mark 设置尾部说明内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 190;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray*Tary=mary[_pageMenu.selectedItemIndex];
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"jzHD" bundle:nil];
    MydetailedController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Mydetailed"];
    secondViewController.cid= WXGStr(Tary[indexPath.row][@"id"]);
    [self.navigationController pushViewController:secondViewController animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
